/**
 * @format
 */

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
import {GestureHandlerRootView} from  'react-native-gesture-handler'
import React, {Component} from 'react';
import {
    AppRegistry,
    ToastAndroid,
    BackHandler,
    Platform,
    UIManager,
    Alert,
    Text,
    TextInput
} from 'react-native';
import App from './src/app';
import {YellowBox} from 'react-native';
YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated', 'Module RCTImageLoader']);


if (Platform.OS === 'android') {
    UIManager.setLayoutAnimationEnabledExperimental &&
    UIManager.setLayoutAnimationEnabledExperimental(true);
}

//字体大小不随系统字体大小变化而变化
TextInput.defaultProps = Object.assign({}, TextInput.defaultProps, {defaultProps: false})
Text.defaultProps = Object.assign({}, Text.defaultProps, {allowFontScaling: false})

export default class PageJump extends Component {

    constructor(props) {
        super(props);
        this.state = {
            selectedTab: 'home',
        }
        this.handleBack = this.handleBack.bind(this);
    }

    componentDidMount() {
        BackHandler.addEventListener('hardwareBackPress', this.handleBack)
    }

    componentWillUnmount() {
        BackHandler.removeEventListener('hardwareBackPress', this.handleBack)
    }

    handleBack = () => {
        if(this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
            Alert.alert(
                '提示',
                '确定要退出应用吗？',
                [
                    {
                        text: '确定',
                        onPress: ()=> this.exitApp()
                    },
                    {
                        text: '取消',
                        onPress: () => console.log('点击取消')
                    }
                ]
            )
            return false
        }
        this.lastBackPressed = Date.now();
        ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT);

        return true;
    }

    exitApp(){
        //最近2秒内按过back键，可以退出应用。
        BackHandler.exitApp();//退出整个应用
    }

    render() {
        return (
            <GestureHandlerRootView style={{flex:1}}>
                <App/>
            </GestureHandlerRootView>

        );
    }
}

AppRegistry.registerComponent('rwater', () => PageJump);
